Skip to main content
Creates a contrastive vision-language model from pretrained weights with optional preprocessing transform. This function enforces loading of pretrained weights and is designed for inference use cases.

Signature

Parameters

str
required
Model identifier, potentially with schema prefix:
  • 'ViT-B-32': Built-in model name. pretrained specifies CLIP weights source (required).
  • 'hf-hub:org/repo': Loads config/weights from HuggingFace Hub. pretrained is IGNORED.
  • 'local-dir:/path/to/folder': Loads config/weights from local directory. pretrained is IGNORED.
Optional[str]
default:"None"
Source for CLIP weights (tag or file path) ONLY if model_name has no schema. If None and schema requires it, will raise an error. Examples: 'openai', 'laion400m_e32', or a file path.
str
default:"'fp32'"
Model precision. Options: 'fp32', 'fp16', 'bf16', 'pure_fp16', 'pure_bf16'.
Union[str, torch.device]
default:"'cpu'"
Device to load model on. Can be 'cpu', 'cuda', or a torch.device object.
bool
default:"False"
If True, JIT compile the model using torch.jit.script.
bool
default:"False"
Force use of QuickGELU activation in model config.
bool
default:"False"
Force use of custom text encoder architecture.
Optional[Union[int, Tuple[int, int]]]
default:"None"
Override image size in model config. Useful for using models at different resolutions than they were trained at.
Optional[int]
default:"None"
Override context length in text config.
Optional[Tuple[float, ...]]
default:"None"
Override default image normalization mean values (per channel). Example: (0.48145466, 0.4578275, 0.40821073).
Optional[Tuple[float, ...]]
default:"None"
Override default image normalization std values (per channel). Example: (0.26862954, 0.26130258, 0.27577711).
Optional[str]
default:"None"
Override default interpolation method for image resizing. Options: 'bicubic', 'bilinear', 'nearest'.
Optional[str]
default:"None"
Override resize mode for inference preprocessing. Options:
  • 'squash': Resize to exact dimensions (may distort aspect ratio)
  • 'shortest': Resize shortest edge to target size, then crop
  • 'longest': Resize longest edge to target size, then crop
Only affects the returned preprocessing transform, not training.
bool
default:"True"
If True, returns (model, preprocess) tuple. If False, returns only the model.
Optional[str]
default:"None"
Cache directory for downloads. Defaults to ~/.cache/clip.
bool
default:"True"
Use weights_only=True for torch.load (safer, prevents arbitrary code execution).
Any
Additional keyword arguments for model constructor (highest override priority).

Returns

torch.nn.Module
The created model instance with pretrained weights loaded.
Callable
Inference preprocessing transform (only returned if return_transform=True). This is a deterministic transform without augmentation, suitable for validation and inference.

Example